package com.ash.print.template;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import lombok.Data;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.util.Arrays;
import java.util.HashMap;

/**
 * 入库标签
 */
public class SpuTemplate implements Printable {

    private final TagInfo tagInfo;

    public SpuTemplate(TagInfo tagInfo) {
        this.tagInfo = tagInfo;
    }


    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
        Graphics2D g2d = (Graphics2D) graphics;
        if (pageIndex > 0) { // 如果不是第一页，则已经打印完毕
            return NO_SUCH_PAGE;
        }

        // 转换坐标系以适应页面格式
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        // 获取可打印区域的宽度和高度
        int width = (int) pageFormat.getImageableWidth();
        int height = (int) pageFormat.getImageableHeight();


        FontMetrics fontMetrics = g2d.getFontMetrics();


        // 设置背景色（可选）
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        g2d.setStroke(new BasicStroke(2f)); // 边框宽度为2个单位

        // 绘制边框
        g2d.setColor(Color.black);

        g2d.drawString("商品码", 85, 20);

        g2d.drawLine(5, 30, width - 15, 30);


        String spuName = "SPU码：" + "123456789123456789-123456789";

        String[] lineNum = wrapText(spuName, (float) (width - 40), fontMetrics);

        System.out.println(Arrays.toString(lineNum));

        int baseY = 45;

        for (int i = 0; i < lineNum.length; i++) {
            baseY = baseY + 15 * i;
            g2d.drawString(lineNum[i], 20, baseY);
        }


        g2d.drawString("商品名称：", 20, baseY + 20);
        g2d.drawString("商品规格：", 20, baseY + 40);

        g2d.drawLine(5, baseY + 55, width - 15, baseY + 55);

        g2d.drawString("用户名：", 20, baseY + 70);
        g2d.drawString("国家：", 20, baseY + 90);
        g2d.drawString("货架号：", 20, baseY + 110);

        g2d.drawLine(5, baseY + 125, width - 15, baseY + 125);

        g2d.drawString("操作员：", 20, baseY + 140);
        g2d.drawString("分拣日期：", 20, baseY + 160);

        g2d.drawRect(5, 5, width - 20, baseY + 180);


        return PAGE_EXISTS;
    }

    private static String[] wrapText(String text, float width, FontMetrics fontMetrics) {
        StringBuilder wrappedText = new StringBuilder();
        String currentLine = "";
        float lineWidth = 0;
        char lastChar = '\n';

        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            int charWidth = fontMetrics.charWidth(c);
            if (c == ' ' || c == '\n') {
                if (lineWidth + charWidth >= width) {
                    wrappedText.append(currentLine.trim()).append('\n');
                    currentLine = "";
                    lineWidth = 0;

                } else {
                    if (!currentLine.isEmpty()) {
                        currentLine += ' ';
                    }
                    lineWidth += charWidth;
                }
            } else {
                if (lastChar != ' ' && lineWidth + charWidth > width) {
                    wrappedText.append(currentLine.trim()).append('\n');
                    currentLine = "" + c;
                    lineWidth = charWidth;
                } else {
                    currentLine += c;
                    lineWidth += charWidth;
                }
            }

            lastChar = c;
        }

        if (!currentLine.isEmpty()) {
            wrappedText.append(currentLine.trim());
        }

        return wrappedText.toString().split("\n");
    }

    @Data
    public static class TagInfo {

        private String spuNo;
        private String spuName;
        private String skuAttr;

        private String userName;
        private String countryText;
        private String shelvesNo;

        private String operatorName;
        private String sortingGoodsDateTime;

    }
}
